/**
 * 日期处理工具类
 * 2019-08-21 
 * taominglong
 */
export class DateUtils {

    /**
     * 格式化日期
     * @param date 
     */
    public static formatDate(date) {
        var y = date.getFullYear();
        var m = date.getMonth() + 1;
        m = m < 10 ? '0' + m : m;
        var d = date.getDate();
        d = d < 10 ? ('0' + d) : d;
        return y + '-' + m + '-' + d;
    }

    /**
     * 格式化日期时间
     * @param date 
     */
    public static formatDateTime(date) {
        var y = date.getFullYear();
        var m = date.getMonth() + 1;
        m = m < 10 ? ('0' + m) : m;
        var d = date.getDate();
        d = d < 10 ? ('0' + d) : d;
        var h = date.getHours();
        h = h < 10 ? ('0' + h) : h;
        var minute = date.getMinutes();
        minute = minute < 10 ? ('0' + minute) : minute;
        var second = date.getSeconds();
        second = second < 10 ? ('0' + second) : second;
        return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second;
    }

    /**
     * 格式化日期为指定的格式
     * @param date 日期
     * @param format 格式
     */
    public static dateToFormat(date, format) {
        var tf = function (i) { return (i < 10 ? '0' : '') + i };
        return format.replace(/yyyy|MM|dd|HH|mm|ss/g, function (a) {
            switch (a) {
                case 'yyyy':
                    return tf(date.getFullYear());
                    break;
                case 'MM':
                    return tf(date.getMonth() + 1);
                    break;
                case 'mm':
                    return tf(date.getMinutes());
                    break;
                case 'dd':
                    return tf(date.getDate());
                    break;
                case 'HH':
                    return tf(date.getHours());
                    break;
                case 'ss':
                    return tf(date.getSeconds());
                    break;
            }
        })
    };

    /**
     * 字符串转时间
     * @param dateStr 
     */
    public static stringToDate(dateStr) {
        var re1 = /^[0-9]{4}-[0-1]{1}[0-9]{1}-[0-3]{1}[0-9]{1}$/;
        var re2 = /^[0-9]{4}-[0-1]{1}[0-9]{1}-[0-3]{1}[0-9]{1}\s[0-2]{1}[0-9]{1}:[0-5]{1}[0-9]{1}:[0-5]{1}[0-9]{1}$/;
        var date;
        if (re1.test(dateStr)) {
            date = dateStr.split("-");
            if (date[1].indexOf("0") == 0) {
                date[1] = date[1].substr(1);
            }
            return new Date(date[0], parseInt(date[1]) - 1, date[2]);
        } else if (re2.test(dateStr)) {
            var re3 = /:/g;
            var re4 = /\s/g;
            dateStr = dateStr.replace(re3, "-").replace(re4, "-");
            date = dateStr.split("-");
            if (date[1].indexOf("0") == 0) {
                date[1] = date[1].substr(1);
            }
            return new Date(date[0], parseInt(date[1]) - 1, date[2], date[3], date[4], date[5]);
        } else {
            return null;
        }
    }
}